package demo

import (
	"encoding/json"
	"fmt"
)

type Level int8

var (
	InfoLevel  Level = 1
	WarnLevel  Level = 2
	ErrorLevel Level = 3
)

type LogModel struct {
	Id    int    `json:"id"`
	Title string `gorm:"size:32"`
	Lever Level  `json:"level"`
}

func (l Level) MarshalJSON() (data []byte, err error) {
	var str string
	switch l {
	case InfoLevel:
		str = "info"
	case WarnLevel:
		str = "warn"
	case ErrorLevel:
		str = "error"
	default:
		str = "unknown"
	}
	return json.Marshal(map[string]any{
		"value": int8(l),
		"label": str,
	})
}
func Enum() {
	Init()
	if err := DB.AutoMigrate(&LogModel{}); err != nil {
		fmt.Println(err)
		return
	}

	//创建
	// DB.Create(&LogModel{Title: "我是标题", Lever: InfoLevel})

	//查询
	var log LogModel
	DB.First(&log)
	jsondata, _ := json.Marshal(log)
	fmt.Println(string(jsondata))
}
